Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 终端设备
    array: [],
    objectArray: [
      {
        id: 0,
        name: ''
      },
      {
        id: 1,
        name: ''
      },
      {
        id: 2,
        name: ''
      },
      {
        id: 3,
        name: ''
      }
    ],
    index: 0,
    deviceId: [],
    currentId: '',
    // 摄像头类型
    casArray: ['海康威视', '浙江大华'],
    casIndex: 0,
    cameraType: '',
    inputValue: '',
    //摄像头功能
    camarafunction: [],
    functionModel: []
  },

  //摄像头类型操作
  bindCasPickerChange: function (e) {
    // console.log(this.data.casArray);
    // console.log('下拉选择的是', this.data.casArray[e.detail.value])
    this.setData({
      casIndex: e.detail.value,
      cameraType: this.data.casArray[e.detail.value]
    })
  },

  //终端设备
  pickerChange: function (e) {
    let that = this;
    var currentId = e.currentTarget.dataset.deviceid;

    that.setData({
      index: e.detail.value,
      currentId: currentId,
    })

    // 设备可用的功能列表
    wx.getStorage({
      key: 'token',
      success: function (res) {
        that.setData({
          token: res.data
        })

        let header = {
          'context-type': 'application/json',
          'UserToken': that.data.token,
          'Cookie': wx.getStorageSync("sessionid")
        }
        wx.request({
          url: 'https://www.2edge.cn/rest/algorithm/devicesAlgorithmList',
          method: 'post',
          header: header,
          data: {
            "userDevicesId": that.data.currentId,
          },
          success: function (res) {
            var camarafunction = res.data.data
            var functionModel = []
            camarafunction.forEach(function (item) {
              functionModel.push(item.model)
              that.setData({
                functionModel: functionModel
              })
            })

            that.setData({
              camarafunction: camarafunction
            })
          },
        })
      }
    })
  },

  // 开始时间
  bindDateChange: function (e) {
    // console.log(e);
    this.setData({
      date: e.detail.value
    })
  },
  // 结束时间
  bind: function (e) {
    // console.log(e);
    this.setData({
      date1: e.detail.value
    })
  },


  // 提交添加的数据
  cameraForms: function (e) {
    let that = this;
    var cameraName = e.detail.value.cameraName;
    var cameraIp = e.detail.value.cameraIp;
    var cameraUserName = e.detail.value.cameraUserName;
    var cameraPsa = e.detail.value.cameraPsa;
    var cameraAddr = e.detail.value.cameraAddr;
    var tell = e.detail.value.tell;
    var functionModel = that.data.functionModel; //摄像头功能
    var currentId = that.data.currentId; //设备ID
    var typeVal = that.data.casIndex; //摄像头类型

    if (currentId === "" || currentId === null) {
      wx.showToast({
        title: '请选择终端设备',
        icon: "loading",
        mask: false
      })
      return;
    }else if (typeVal === "" || typeVal === null) {
      wx.showToast({
        title: '请选择摄像头类型',
        icon: "loading",
        mask: false
      })
      return;
    };

    wx.getStorage({
      key: 'token',
      success: function (res) {
        that.setData({
          token: res.data
        })

        let header = {
          'context-type': 'application/json',
          'UserToken': that.data.token,
          'Cookie': wx.getStorageSync("sessionid")
        }
        wx.request({
          url: 'https://www.2edge.cn/rest/camera/insert',
          method: 'post',
          header: header,
          data: {
            "deviceId": currentId,
            "name": cameraName,
            "ip": cameraIp,
            "account": cameraUserName,
            "password": cameraPsa,
            "port": cameraAddr,
            "type": typeVal,
            "functionsItem": functionModel,
            "drawData": "",
            "mobile": tell
          },
          success: function (res) {
            // console.log(res);
            wx.redirectTo({
              url: '../camera/camera',
            })
          },
          fail: function () {
            console.log("添加失败");
          }
        })
      }
    })

  },


  /**
  * 生命周期函数--监听页面加载
  */
  onLoad: function (options) {
    let that = this;
    // 终端设备—列表
    wx.getStorage({
      key: 'token',
      success: function (res) {
        that.setData({
          token: res.data
        })

        let header = {
          'context-type': 'application/json',
          'UserToken': that.data.token,
          'Cookie': wx.getStorageSync("sessionid")
        }
        wx.request({
          url: 'https://www.2edge.cn/rest/userDevices/queryList',
          method: 'post',
          header: header,
          data: {
            "pageNum": "1",
            "pageSise": "10",
            "conditions": []
          },
          success: function (res) {
            // console.log(res);

            var arr = [] //店名
            var deviceIdArray = [] //设备ID
            var deviceArr = res.data.data.records;

            deviceArr.forEach(function (item) {
              var deviceName = item.locationName + item.name
              deviceIdArray.push(item.id)
              arr.push(deviceName)

              that.setData({
                array: arr,
                deviceId: deviceIdArray
              })
            });

            // console.log(deviceIdArray);
            // console.log(that.data.deviceId);


          },
          fail: function () {
            console.log("获取设备ID失败");
          }
        })
      }
    })
  }
})